import requests
import pandas as pd
from fastapi import APIRouter
import uvicorn
from httpx import AsyncClient
import config_read

weather_api = APIRouter()

df = pd.read_excel('AMap_adcode_citycode.xlsx')
key = config_read.config_read('gaode_api')


@weather_api.get('/get')
async def get_weather(city: str):
    city_code = df[df['中文名'].str.contains(city)]['adcode'].iloc[0]

    if city_code:
        url = f'https://restapi.amap.com/v3/weather/weatherInfo?city={city_code}&key={key}'
        # re = requests.get(url)
        async with AsyncClient() as ac:
            re = await ac.get(url)
            return re.json()
